package com.linzm.leetcode.primary.exercises2_20230116;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @Author zimingl
 * @Date 2023/1/20 20:54
 * @Description: 删除字符使字符串变好
 */
public class Demo12_1958 {
    /**
     * 一个字符串如果没有 三个连续 相同字符，那么它就是一个 好字符串 。
     * 给你一个字符串 s ，请你从 s 删除 最少 的字符，使它变成一个 好字符串 。
     * 请你返回删除后的字符串。题目数据保证答案总是 唯一的 。
     * 输入：s = "leeetcode"
     * 输出："leetcode"
     * 解释：
     * 从第一组 'e' 里面删除一个 'e' ，得到 "leetcode" 。
     * 没有连续三个相同字符，所以返回 "leetcode" 。
     */
    public static void main(String[] args) {
        Demo12_1958 demo12_1958 = new Demo12_1958();
        String s = "leeetcode";
//        String s = "abbbb";
//        String s = "bbbb";
        StringBuilder stringBuilder = new StringBuilder(s);
        stringBuilder.charAt(0);
        String makeFancyString = demo12_1958.makeFancyString3(s);
        System.out.println(makeFancyString);
    }

    private String makeFancyString(String s) {
        // aaa
        // abbb
        Deque<Character> stack = new LinkedList<>();
        int count = 0;
        String makeFancyString = "";
        if (s.length() != 0) {
            stack.push(s.charAt(0));
        }
        for (int i = 1; i < s.length(); i++) {
            if (count > 0 && stack.peek() != s.charAt(i)) {
                count = 0;
            }
            if (stack.peek() == s.charAt(i)) {
                count++;
            }
            if (stack.peek() != s.charAt(i) || (stack.peek() == s.charAt(i) && count < 2)) {
                stack.push(s.charAt(i));
            }
        }
        while (!stack.isEmpty()) {
            makeFancyString = stack.pop() + makeFancyString;
        }
        return makeFancyString;
    }

    private String makeFancyString2(String s) {
        // aaa
        // abbb
        StringBuilder stringBuilder = new StringBuilder();
        int count = 0;
        if (s.length() != 0) {
            stringBuilder.append(s.charAt(0));
        }
        for (int i = 1; i < s.length(); i++) {
            if (count > 0 && getStringLast(stringBuilder) != s.charAt(i)) {
                count = 0;
            }
            if (getStringLast(stringBuilder) == s.charAt(i)) {
                count++;
            }
            if (getStringLast(stringBuilder) != s.charAt(i) || (getStringLast(stringBuilder) == s.charAt(i) && count < 2)) {
                stringBuilder.append(s.charAt(i));            }
        }
        return stringBuilder.toString();
    }

    private Character getStringLast(StringBuilder stringBuilder) {
        int length = stringBuilder.length();
        return stringBuilder.charAt(length - 1);
    }

    private String makeFancyString3_bak(String s) {
        StringBuilder stringBuilder = new StringBuilder();
        if (s.length() < 3) {
            return s;
        }
        char tmp = s.charAt(0);
        int num = 1;
        stringBuilder.append(tmp);
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == tmp) {
                num++;
                if (num < 3) {
                    stringBuilder.append(s.charAt(i));
                }
            } else {
                tmp = s.charAt(i);
                stringBuilder.append(tmp);
                num = 1;
            }
        }
        return stringBuilder.toString();
    }

    public String makeFancyString3(String s) {
        StringBuffer sb =  new StringBuffer();
        int len = s.length();
        if(len < 3){
            return s;
        }
        int num = 1;
        char pre = s.charAt(0);
        sb.append(pre);
        for(int i = 1;i < len;i++){
            char c  = s.charAt(i);
            if(c == pre){
                num++;
                if(num < 3){
                    sb.append(c);
                }
            }else{
                pre = s.charAt(i);
                sb.append(c);
                num = 1;
            }
        }
        return sb.toString();
    }
}
